function AdminManager ( attrs )
{
	var self = this;

	var name;
	if ( "name" in attrs )
		name = attrs [ 'name' ];
	else
		name = "am-" + AdminManager._instances.count ();


	AdminManager._instances [ name ] = this;

	this.templates = AdminManager._templates.clone ();

	var tmpl;
	if ( ( tmpl = attrs.get ( "templates" ) ) ) 
	{
		this.templates.inherits ( tmpl, true );
		console.debug ( "AdminManager.templates = %o" , this.templates );
	}

	this.highlight_class = attrs.get ( 'highlight_class','select' ); 
	this.highlight_color = attrs.get ( 'highlight_color', null );
	this._ds = null;
	this._selected_row = {};
	this._am_name = name;
	this._lines_per_page = attrs.get ( 'lines_per_page', 10 );
	this._dest_res = attrs.get ( 'dest_res', 'block_main' );
	this._list_action = attrs.get ( 'list_action' );		//action: comando server da chiamare per fare lista
	this._del_action = attrs.get ( 'del_action' );			//action: comando server da chiamare per eliminare
	this._pos_action = attrs.get ( 'pos_action' );			//action: comando server da chiamare per eliminare
	this._filter_form = attrs.get ( 'filter_form' );		//filter form
	this._filter_form_name = attrs.get ( 'filter_form_name' );		//filter form
	this._selected = {};
	this._int_res_div = this._am_name + ":results"; 
	this._int_cnt_div = 'cnt:' + this._int_res_div;
	this._mod_menu_actions = attrs.get ( 'mod_menu_actions', [] ); //structure: [{ func: , img_src: , label: }]
	this._glob_menu_actions = attrs.get ( 'glob_menu_actions', [] ); //structure: [{ action_name: , img_src: , label:, func:  }];
	this._glob_actions = {};
	
	this._pops = {}; // popboxes
	this._sort_val = {};
	this._row_fields = null;
	this._row_actions = null;

	this._def_ds_sort_cback = 'AdminManager.sort_data';
	this._def_ds_filter_cback = 'AdminManager.filter_data';
	this._def_ds_filter_form = AdminManager.filter_data_form;

	this._def_pos = 'pos';	// default name for ordering column ( set to null to disable this function )

	this._parse_rows_sess = null; // cache id _parse_table_row
	this._parse_rows_cache = {}; 
 
	this.cbacks = {
		'row_manip': null,
		'show_results': null,
		'edit_item': null, 
		'del_item': function ( id ) { self.del ( id ); }, 

		'pre_del' : null,		// pre_del ( object ) - If returns FALSE, deletion will not take place
		'post_del' : null,		// post_del ( object ) - callback defined after deletion successfully completed
		'noresult' : null,
		'position' : null,
		'ds_sort' : {},			// params:  { field ( am ) : { [ field ( db ) ], [ cback function ] } }
		'ds_filter' : {},		// params:  { filed ( am ) : { [ form ( func ) ], [ field ], [ cback function ] } }
		'ds_fill_start' : null,
		'ds_fill_end' : null,
		'generate_id' : attrs.get ( "generate_id" ),
		'on_row_click' : null
	};

	return this;
};

AdminManager._instances = {};

AdminManager.prototype.set_row_fields = function ( fields )
{
	this._row_fields = fields;
};

AdminManager.prototype.set_row_actions = function ( actions )
{
	this._row_actions = actions;
};

AdminManager.prototype.set_dest_div = function ( div_id )
{
	this._dest_res = div_id;
};


AdminManager.prototype.show_list = function ( dest_div, search_params )
{
	var self = this;

	this._init_ds ();

	this._selected = {};

	if ( dest_div ) this._dest_res = dest_div;

	$( this._dest_res, '' );

	var cnt = document.createElement ( 'div' );
	var cnt_filter = document.createElement ( 'div' );

	cnt_filter.setAttribute ( 'id', self._int_cnt_div + ":filter" );
	cnt_filter.setAttribute ( 'class', 'adminman_content_filter' );
	cnt.setAttribute ( 'id', self._int_cnt_div );
	cnt.setAttribute ( 'class', 'adminman_content_results' );

	cnt._admin_man = self;

	$( this._dest_res ).appendChild ( cnt_filter );
	$( this._dest_res ).appendChild ( cnt );

	$( self._int_cnt_div, String.formatDict ( this.templates [ "search_results" ], this ) );

	if ( self._filter_form ) self._filter_form ( self._int_cnt_div + ':filter' );

	this.search_start ( search_params );
};

/* NOTE: ds_filter form must be called [am name]_[field]_filter_form */
AdminManager.prototype._sort_cbacks = function ( v )
{
	var cback, form, field_cbacks = '';
	var sort = this.cbacks [ 'ds_sort' ];
	var cond = this.cbacks [ 'ds_filter' ];
	var dct = {
			_field : v [ 1 ],
			_am_name : this._am_name
			};

	if (  ! sort.get ( v [ 1 ] ) && ! cond.get ( v [ 1 ] ) ) return field_cbacks;

	if ( sort.get ( v [ 1 ] ) ) 
	{
		dct [ 'cback' ] = sort [ v [ 1 ] ].get ( 'cback', this._def_ds_sort_cback ); 
		field_cbacks = String.formatDict ( this.templates [ 'ds_sort_cback' ], dct ); 
	}
	if ( cond.get ( v [ 1 ] ) ) 
		field_cbacks += String.formatDict ( this.templates [ 'ds_filter_cback' ], dct );

	return field_cbacks;
};

AdminManager.prototype.pop_pos_form = function ( id, name, ds_pos )
{
	var ext = name + '_' + id;
	var pop_name = 'pop_' + ext
	var pop_cnt = 'popbox:' + pop_name;
	var pop = this._pops.get ( pop_name );
	var parent_div = 'parent_pos_' + ext;
	
	var item = this._ds.get_row ( ds_pos );

	if ( ! pop )
	{
		this.clear_popbox ();
		var f = new liwe.form.instance ( 'form_' + pop_name ); 
		var label = 'Sposta da posizione ' + item [ this._def_pos ] + ' a ';
		f.label ( { 'value' : label, 'colspan': 4  } );
		f.number ( { 'name' : 'pos', 'value' : '', 'colspan': 4 } );
		f.hidden ( { 'name' : 'id', 'value' : id } );
		f.hidden ( { 'name' : 'name', 'value' : name } );
		f.hidden ( { 'name' : 'ds_pos', 'value' : ds_pos } );
		f.button ( { 'name' : 'move_butt', 'value' : 'invia', 'nonl' : true, 'class': 'green', 
				'onclick' : "AdminManager._do_move_field ( '" + id + "', '" + name + "' )"  } );
		f.button ( { 'name' : 'cancel_butt', 'value' : 'cancel', 'class': 'gray',
				'onclick' : "AdminManager.pop_clear ( '" + name + "', '" + pop_name + "' )"  } );
		pop = this.popbox ( pop_name, $( parent_div ), 180, 100 );
		pop.show ();
		var form_html = f.get ();
		$( pop_cnt, form_html );
		return;
	};
	if ( $( pop_cnt ).style.display == 'block' ) 
	{
		pop.hide ();
		return;
	}
	pop.set_parent ( parent_div );
	pop.show ();
};

AdminManager.prototype.pop_filter_form = function ( field, name )
{
	var pop_name = 'pop_' + name + '_' + field;
	var pop_cnt = 'popbox:' + pop_name;
	var pop = this._pops.get ( pop_name );
	var parent_div = 'filter_' + name + '_' + field;

	if ( ! pop )
	{
		pop = this.popbox ( pop_name, $( parent_div ) );
		pop.show ();
		var form = this.cbacks.ds_filter [ field ].get ( 'form' , this._def_ds_filter_form );
		var form_html = form ( field, name );
		$( pop_cnt, form_html );
		return;
	};
 
	if ( $( pop_cnt ).style.display == 'block' ) 
	{
		pop.hide ();
		return;
	}
	pop.set_parent ( parent_div );
	pop.show ();
};

/* ------------- highlight rows functions -----------------*/
AdminManager.prototype._parse_table_rows = function ( cback, el, dest_div )
{
	if ( ! dest_div ) dest_div = this._int_res_div;
	if ( ! el ) el = 'tr';
	var tbl = $( dest_div );
	var rows = $c( el, 'adminman_row', tbl );  
	var from_row = this._ds.from_row;
	var c, row, pos = 0, l = rows.length;
	for ( c = 0; c < l; c += 1 )
	{
		row = rows [ c ];
		//console.debug ( "pos = %s, cback = %s, selected = %s\n" , pos , cback.toString (), this._selected_row );
		if ( cback ) cback ( row, from_row + pos, dest_div );
		pos += 1;
	}
};

AdminManager.prototype._clear_selected_row = function ()
{
	this._selected_row = {};
};

AdminManager.prototype._set_selected_row = function ( row, pos, dest_div, click )
{
	var selected = this._selected_row.get ( row._highlight );
	this._clean_highlighted_all ( dest_div );
	this._clear_selected_row ();
	//if alreday selected then unselect it and return
	if ( ! selected || ! click ) 
	{
		this._selected_row [ row._highlight ] = this._ds.get_row ( pos ); //row._highlight;
		if ( this.highlight_color )
		{
			row.style.backgroundColor = this.highlight_color;
			return;
		}
		liwe.dom.add_class ( row , this.highlight_class );
	}
	var row_click = this.cbacks [ 'on_row_click' ];
	if ( row_click ) row_click ( this );
};

AdminManager.prototype._clean_highlighted_row = function ( row )
{
	if ( this.highlight_color )
	{
		row.style.backgroundColor = 'none';
	};
	liwe.dom.del_class ( row, this.highlight_class );
};

AdminManager.prototype._clean_highlighted_all = function ( dest_div )
{
	var self = this;
	this._parse_table_rows ( function ( row, pos )
				{
					self._clean_highlighted_row ( row );
				} );
};

AdminManager.prototype._add_row_onclick = function ( dest_div )
{
	var _row_val, self = this;
	this._parse_table_rows ( function ( row, pos )
			{
				liwe.events.add ( row, 'click', function () 
							{ 
								self._set_selected_row ( row, pos, dest_div, click = true );
							} );
				row._highlight = row.id;
				if ( ! self._selected_row.get (  row._highlight ) )
				{
					self._clean_highlighted_row ( row );					
					return;
				}			
				self._set_selected_row ( row, pos, dest_div );
			} );
};

AdminManager.prototype.get_selected_row = function ()
{
	return this._selected_row.values () [ 0 ];
};

AdminManager.prototype.filter_data = function ( field, name )
{
	var form_name = name + '_' + field + '_filter_form';
	var form = liwe.form.get ( form_name );
	var vals = form.get_values ();
	var def_field = this.cbacks [ 'ds_filter' ] [ field ].get ( 'field', field );
	var ds_cond_val = def_field + '|' + vals.cond;		
	var f = liwe.form.get ( this._filter_form_name );
	f.set_value ( 'ds_cond', ds_cond_val );
	var pop_name = 'pop_' + name + '_' + field;
	this.clear_popbox ();
	this.search_start ();
};

AdminManager.prototype._create_templates = function ()
{
	var self = this;

	var s = new String.buffer ();
	var s2 = new String.buffer ();
	var sort_cback, field_cbacks;

	s.add ( this.templates [ 'ds_table_head' ] );
	s.add ( this.templates [ 'ds_th_header_start' ] );
	s.add ( String.formatDict ( this.templates [ 'ds_th_checkbox' ], { _am_name: this._am_name } ) );

	s2.add ( this.templates [ 'ds_td_checkbox' ] );
	//s2.add ( "%(_checkbox)s" );

	this._row_fields.iterate ( function ( v , k )
				{
					field_cbacks = self._sort_cbacks ( v );
					s.add ( String.formatDict ( self.templates [ 'ds_th_single_field' ], { '_row_field' : v  [ 0 ], 'field_cbacks' : field_cbacks } ) );
					s2.add ( String.formatDict ( self.templates [ 'ds_td_single_field' ], { '_row_field' : '%(' + v [ 1 ] + ')s' } ) );
				} );
	s.add ( this.templates [ 'ds_th_close' ] );
	s2.add ( this.templates [ 'ds_td_close' ] );

	return [ s.toString (), s2.toString () ];
};

AdminManager.prototype._init_ds = function ()
{
	var self = this;
	var templ = this._create_templates ();

	if ( ! this._ds )
		this._ds = new DataSet ( this._am_name + "_ds_list", "/ajax.pyhp" );

	this._ds.lines_per_page = this._lines_per_page;

	this._ds.templates [ 'table_start' ] = templ [ 0 ];
	this._ds.templates [ 'table_header' ] = "";
	this._ds.templates [ 'table_footer' ] = "";
	
	this._ds.templates [ 'table_row' ] = templ [ 1 ]; 
	this._ds.templates [ 'prev_page' ] = self.templates [ 'ds_prev_page' ];
	this._ds.templates [ 'next_page' ] = self.templates [ 'ds_next_page' ];

	this._ds.cbacks [ 'row_manip' ] = function ( ds, row ) { self._row_manip ( ds, row ); };
	this._ds.cbacks [ 'show_results' ] = function ( ds ) { self._ds_show_results ( ds ); };
	this._ds.cbacks [ 'generate_id' ] = this.cbacks [ 'generate_id' ];
	this._ds.cbacks [ 'fill_start' ] = this.cbacks [ 'ds_fill_start' ];
	this._ds.cbacks [ 'fill_end' ] = this.cbacks [ 'ds_fill_end' ] ;
};

AdminManager.prototype._create_actions = function ( row, custom )
{
	var self = this;
	var s = new String.buffer ();
	var tmp, tmp_tpl;

	row [ '_am_name' ] = self._am_name;

	s.add ( this.templates [ 'actions_head' ] );
	tmp = String.formatDict ( this.templates [ 'edit_obj' ], row );
	s.add ( String.formatDict ( this.templates [ 'actions_cell' ], { '_action' : tmp } ) );
	tmp = '';

	if ( this._row_actions )
	{
		this._row_actions.iterate ( function ( el )
		{
			tmp = String.formatDict ( self.templates [ 'actions_row_action' ], 
							{ 	'_id' : row._id || row.id ,
								'_action' : el [ 1 ],
								'_txt' : el [ 0 ],
								'_am_name' : row [ '_am_name' ]
							   } ); 
			s.add ( String.formatDict ( self.templates [ 'actions_cell' ], { '_action' : tmp } ) );
		} );
	}

	tmp = String.formatDict ( this.templates [ 'del_obj' ], row );
	s.add ( String.formatDict ( this.templates [ 'actions_cell' ], { '_action' : tmp } ) );
	// custom row actions ( position )
	if ( custom ) s.add ( String.formatDict ( self.templates [ 'actions_cell' ], { '_action' : custom } ) );
	s.add ( this.templates [ 'actions_footer' ] );

	return s.toString ();
};

AdminManager.prototype._positioning_tmpl = function ( row )
{
	if ( ! this.cbacks.get ( 'position' ) ) return;
	var tmpl = String.formatDict ( this.templates [ 'am-move-field' ], row );
	return tmpl;
};

AdminManager.prototype._row_manip = function ( ds, row )
{
	row [ '_am_name' ] = this._am_name;
	row [ '_checked' ] = '';
	row [ '_style' ] = '';

	row [ '_txt' ] = 'Specificare il template della row';
	row [ '_created' ] = '';


	if ( this._selected.get ( row [ 'id' ] ) )
	{
		row [ '_checked' ] = 'checked="checked"';
		row [ '_style' ] = 'style="border: 1px solid red;"';
	}

	if ( this.cbacks [ 'row_manip' ] ) this.cbacks [ 'row_manip' ] ( ds, row );

	custom = ( row.get ( this._def_pos ) ) ? this._positioning_tmpl ( row ) : null;
	row [ '_acts' ] = this._create_actions ( row, custom );
};

AdminManager.prototype._ds_show_results = function ( ds )
{
	var self = this;
	var tot = ds.num_rows;
	var pages = 0;
	if ( tot ) pages = Math.floor ( ( tot - 1 ) / ds.lines_per_page ) + 1;
	var p_next = ds.page + 1;
	var p_prev = ds.page - 1;
	var page = ds.num_rows ? ds.page + 1 : '';

	var buff = new String.buffer ();

	var dct = { 
		    '_glob_delete' : String.formatDict ( this.templates [ 'glob_delete' ] , { _am_name : this._am_name } ),
		    '_glob_refresh' : String.formatDict ( this.templates [ 'glob_refresh' ] , { _am_name : this._am_name } )
		};

	buff.add ( String.formatDict ( this.templates [ 'glob_actions' ], dct ) );
	this._glob_menu_actions.iterate ( function ( act, t )
					{
						act [ '_am_name' ] = self._am_name;
						if ( t == 0 ) buff.add (  self.templates [ 'mod_menu_sep' ] );
						if ( act.get ( 'sep' ) ) buff.add (  self.templates [ 'mod_menu_sep' ] );
						else buff.add ( String.formatDict ( self.templates [ 'glob_single_action' ], act ) );

						self._glob_actions [ act.action_name ] = act.func;
					} );

	this._mod_menu_actions.iterate ( function ( act , t )
					{
						act [ '_am_name' ] = self._am_name;
						if ( t == 0 ) buff.add ( self.templates [ 'mod_menu_sep' ] );

						if ( act.get ( 'sep' ) ) buff.add ( self.templates [ 'mod_menu_sep' ] );
						else buff.add ( String.formatDict ( self.templates [ 'module_single_action' ], act ) );
					} );

	var acts = buff.toString ();
	var dct = {
		'_am_name' : self._am_name,
		'page': page,
		'tot_pages': ds.num_rows ? ' di ' + pages : '',
		'num_rows': ds.num_rows,
		'next_page': (p_next > 0 && ( ds.page < pages -1 ) ) ?  'DataSet.page(\'' + ds.name + '\',' + p_next + ')' : '',
		'prev_page': ( ds.page > 0 ) ? 'DataSet.page(\'' + ds.name + '\',' + p_prev + ')' : '',
		'disp_next': ( p_next > 0 && ( ds.page < pages - 1 ) ) ? "" : "_disable",
		'disp_back': ( ds.page > 0 ) ? "" : "_disable",
		'_glob_actions': acts 
	};

	$ ( this._am_name + ':results_header' ).innerHTML = String.formatDict ( this.templates [ 'results_toolbar' ], dct );
	$ ( this._am_name + ':results_footer' ).innerHTML = String.formatDict ( this.templates [ 'results_toolbar' ], dct );

	this._add_row_onclick ();
	if ( this.cbacks [ 'show_results' ] ) this.cbacks [ 'show_results' ] ( ds );
};

AdminManager.prototype._sort_arrow = function ( current )
{
	var self = this;
	var sort_id, flag;

	if ( ! current ) return;

	if ( current.length || current.split ( '|' ) [ 0 ] ) current = current.split ( '|' ) [ 0 ];

	this.cbacks [ 'ds_sort' ].iterate ( function ( v, k )
				{
					sort_id = 'sort_' + self._am_name + '_' + k;
					flag = self.cbacks.ds_sort [ k ].get ( 'field', k );
					if ( ! $( sort_id ) || ! self._sort_val.get ( k ) ) return;
					// reset arrows style on other columns
					if ( flag != current ) 
					{
						liwe.dom.del_class ( $( sort_id ), 'th-asc' );
						liwe.dom.del_class ( $( sort_id ), 'th-desc' );
						delete self._sort_val [ k ];
						return;
					}
					// set arrow style to current selected
					liwe.dom.add_class ( $( sort_id ), 'th-' + self._sort_val [ k ] );
				} );
};

AdminManager.prototype.search_start = function ( custom_params )
{
	var self = this;
	// reset selected row
	this._clear_selected_row ();

	var dict = {};
	if ( this._filter_form_name )
	{
		var f =  liwe.form5.get ( this._filter_form_name );
		if ( typeof ( f ) == 'undefined' )
		{
			f = liwe.form.get ( this._filter_form_name );
			if ( ! typeof ( f ) == "undefined" ) console.warn ( "AdminManager: form is deprecated unse form5" );
		}

		if ( f ) dict = f.get_values ();
	}

	if ( custom_params )
	{
		dict.inherits ( custom_params );
		if ( typeof ( dict.get ( 'parent' ) == 'object' ) ) delete dict.parent;
	}

	dict [ 'action' ] = this._list_action;

	this._ds.set_fields ( dict );
	this._ds.fill ( function ( ds ) {
		if ( ds.num_rows <= 0 )
			if ( self.cbacks [ 'noresult' ]  )
				self.cbacks [ 'noresult' ] ();
			else
				$ ( self._int_res_div ).innerHTML = self.templates [ 'no_result' ];
			
		else ds.render ( self._int_res_div );
		self._sort_arrow ( dict.get ( 'ds_sort', '' ) );
	} );
};

AdminManager.prototype._show_results = function  ( ds )
{
	if ( ds.num_rows <= 0 )
		if ( self.cbacks [ 'noresult' ]  )
			self.cbacks [ 'noresult' ] ();
		else
			$ ( this._int_res_div ).innerHTML = this.templates [ 'no_result' ];
	else
		ds.render ( this._int_res_div );
};

AdminManager.prototype.get_selected = function ()
{
	// return this._ds.get_selected ();
	var _selected, tmp_id, ids = [];
	var c = 0, self = this;

	_selected = this._ds.get_selected ();

	_selected.iterate ( function ( v ) {
					tmp_id = v.get ( '_id' );
					ids.push ( tmp_id );
					self._selected [ tmp_id ] = v;
				} );
	c = ids.length;
	if ( ! c )
	{
		return -1;
	}

	return ids;
}

AdminManager.prototype.del_all = function ()
{
	var ids = this.get_selected ();
	if ( ids == -1 ) 
	{
		alert ( 'Nessun elemento selezionato' );
		return;
	};
	ids = "|".join ( ids )

	this.del ( ids );
};

AdminManager.prototype.del = function ( ids )
{
	var self = this;

	if ( ! ids ) return false;

	if ( ( this.cbacks [ 'pre_del' ] ) && ( this.cbacks [ 'pre_del' ] ( this ) == false ) ) return false;

	ids = "" + ids;

	var msg = "Sei sicuro di voler eliminare l'elemento?";
	if ( ids.indexOf ( "|" ) != -1 ) msg = "Sei sicuro di voler eliminare " + ids.split ( "|" ).length + " elementi?";

	if ( confirm ( msg ) )
	{
		liwe.AJAX.easy ( { action: this._del_action, ids: ids }, function ( v )
		{
			if ( self.cbacks [ 'post_del' ] ) self.cbacks [ 'post_del' ] ( self, v );
			self._ds.refresh ();
		} );
	}
};

AdminManager.prototype.clear = function ()
{
	self._ds.clear_selected ();
	this._selected = {};
};

AdminManager.prototype.select_all = function ( selected )
{
	var self = this;
	var chk, tmp_el, els = $c( 'td', 'am_check', $( this._int_res_div ) );

	els.iterate ( function ( el )
			{
				tmp_el = el.firstChild; 
				tmp_el.checked = selected;
				self._ds.set_selected ( tmp_el.value, selected );
			} );
};

AdminManager.prototype.destroy = function ()
{
	$( this._int_cnt_div )._admin_man = null;
};

AdminManager.prototype.popbox = function ( name, parent_div, w, h )
{
	
	if ( ! w ) w = 200;
	if ( ! h ) w = 100;
	
	if ( ! name ) name = 'pop_' + this._am_name;
	if ( this._pops.get ( name ) ) return;
	pop = new WWL.popbox ( name );
	pop.set_size ( w, h );
	pop.set_parent ( parent_div, 'L' );
	this._pops [ name ] = pop;
	return pop;
};

AdminManager.prototype.clear_popbox = function ( name )
{
	if ( name )
	{
		$this._pops [ name ].destroy ();
		delete self._pops [ name ];
		return;
	}
	var self = this;
	this._pops.iterate ( function ( v, k )
				{
					self._pops [ k ].destroy ();
					delete self._pops [ k ];
				} );
};

AdminManager.prototype.refresh_page = function ( cback )
{
	this._ds.refresh ( cback );
};

AdminManager.clear = function ( name )
{
	var am = AdminManager._get_instance ( name );
	am.clear ();
	am.show_list ();
};

AdminManager.select_all = function ( selected, name )
{
	var am = AdminManager._get_instance ( name );
	am.select_all ( selected );
};

AdminManager.refresh = function ( name )
{
	var am = AdminManager._get_instance ( name );
	am.show_list ();
};

AdminManager._get_instance = function  ( name )
{
	return AdminManager._instances [ name ];
};

AdminManager.obj_inp_click = function ( el, id, selected, name )
{
	var am = AdminManager._get_instance ( name );

	am.set_selected ( el, id, selected );
};

AdminManager.del_all_item = function ( name )
{
	var am = AdminManager._get_instance ( name );

	am.del_all ();
};

AdminManager.glob_action = function ( act_name, name )
{
	var am = AdminManager._get_instance ( name );

	var ids = am.get_selected ();
	if ( ids == -1 ) 
	{
		alert ( 'Nessun elemento selezionato' );
		return;
	}

	var func = am._glob_actions.get ( act_name );

	func && func ( ids );
};

AdminManager.row_action = function ( id, action, name )
{
	var am = AdminManager._get_instance ( name );
	var pre_cback = null, post_cback = null;

	switch ( action )
	{
		case "del_item":
			pre_cback = am.cbacks [ 'pre_del' ];
			post_cback = am.cbacks [ 'post_del' ];
			break;
	}

	if ( pre_cback && ( pre_cback ( am ) == false ) ) return;
	if ( am.cbacks [ action ] ) am.cbacks [ action ] ( id );
	if ( post_cback ) post_cback ( am );
};

AdminManager.filter_data = function ( field, name )
{
	var form = name + '_' + field + '_filter_form';
	var am = AdminManager._get_instance ( name );
	am.filter_data ( field, name );
};

AdminManager.filter_data_form = function ( field, name )
{
	var am = AdminManager._get_instance ( name );
	var field_val = am.cbacks [ 'ds_filter' ] [ field ];
	var cback = field_val.get ( 'cback', am._def_ds_filter_cback );
	var cback_str = String.formatDict ( "%(cback)s ( '%(field)s', '%(name)s' );", 
				{ 'cback': cback, 'field': field, 'name': name } ); 
	var form_name = name + '_' + field + '_filter_form';
	var f = new liwe.form.instance ( form_name );
	f.label ( { value: 'Condizione: ' } );
	f.text ( { name: 'cond', value: '' } );
	f.button ( { name: 'field_search', value: 'filtra', onclick: cback_str } );
	return f.get ();
};

AdminManager.pop_clear = function ( name, popbox )
{
	var am = AdminManager._get_instance ( name );
	var pop = am._pops.get ( popbox );
	pop.hide ();
	am.clear_popbox ( popbox );	
};

AdminManager.pop_filter_form = function ( field, name )
{
	var am = AdminManager._get_instance ( name );
	am.pop_filter_form ( field, name );
};

AdminManager.pop_pos_form = function ( id, name, pos )
{
	var am = AdminManager._get_instance ( name );
	am.pop_pos_form ( id, name, pos );
};
AdminManager.sort_data = function ( field, name )
{
	var am = AdminManager._get_instance ( name );
	var current_sort_val = ( am._sort_val.get ( field, 'asc' ) == 'asc' ) ? 'desc' : 'asc';
	am._sort_val [ field ] =  current_sort_val;
	var ds_sort_val = am.cbacks.ds_sort [ field ].get ( 'field', field ) + '|' + current_sort_val;
	var f, custom;
	if ( f = liwe.form.get ( am._filter_form_name ) )
		f.set_value ( 'ds_sort', ds_sort_val );
	else 
		custom = { 'ds_sort' : ds_sort_val };
	
	am.search_start ( custom );
};

AdminManager._do_move_field = function ( id, name )
{
	var ext = name + '_' + id;
	var pop_name = 'pop_' + ext;
	var form_name = 'form_' + pop_name;
	var f = liwe.form.get ( form_name );
	var vals = f.get_values ();
	AdminManager.move_field ( vals.id, vals.name, vals.ds_pos, '', vals.pos );
	AdminManager.pop_clear ( name, pop_name );
};

AdminManager.move_field = function ( id, name, ds_pos, dir, new_pos )
{
	var am = AdminManager._get_instance ( name );
	var row = am._ds.get_row ( ds_pos );
	var pos = row [ am._def_pos ];
	var id_categ = row [ '_id_categ' ];
	
	if ( new_pos )
	{
		am.cbacks [ 'position' ] ( id, id_categ, new_pos );	
		return;
	}
		
	var curr_dir = am._sort_val.get ( am._def_pos, 'desc' );
	//var curr_dir = 'desc';
	if ( dir == 'down' ) pos = ( curr_dir == 'desc' ) ? pos -= 1 : pos += 1;
	if ( dir == 'up' ) pos = ( curr_dir == 'desc' ) ? pos += 1 : pos -= 1;
	if ( pos < 1 ) return;
	//if ( pos < 0 ) return;
	am.cbacks [ 'position' ] ( id, id_categ, pos );	
};

AdminManager.get_selected_row = function ( name )
{
	var am = AdminManager._get_instance ( name );
	return am.get_selected_row ();
};

AdminManager.clear_selected_row = function ( name )
{
	var am = AdminManager._get_instance ( name );
	am._clear_selected_row ();
};
